<!--
 * @Author: your name
 * @Date: 2021-04-08 19:07:19
 * @LastEditTime: 2021-05-15 18:23:14
 * @LastEditors: Please set LastEditors
 * @Description: 条形图2
 * @FilePath: /medicine-web/src/components/Charts/BarChart2.vue
-->
<template>
  <div class="bar-chart2-container">
    <div :id="elementId" style="height: 360px;"></div>
  </div>
</template>
<script>
import * as echarts from 'echarts';
export default {
  name: 'barChart2',
  data () {
    return {}
  },
  mounted () {
    this.initChart()
  },
  methods: {
    initChart () {
      var myChart = echarts.init(document.getElementById(this.elementId))
      myChart.setOption({
        title: {
          text: this.chartTitle,
          textStyle: {
            color: '#555555',
            fontSize: 14,
            fontWeight: 'bold'
          }
        },
        grid: {
          left: '0%',
          right: '5%',
          bottom: '30px',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          splitLine: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: false
          }
        },
        yAxis: {
          type: 'category',
          data: this.getYAxis(),
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          }
        },
        series: {
          name: '机制/文献类型',
          type: 'bar',
          barWidth: 30,
          data: this.getData(),
          label: {
            show: true,
            position: 'right',
            color: '#AFAFAF'
          },
          itemStyle: {
            color: function (param) {
              let color = '#8DC8FF'
              switch (param.name) {
                case '英文文献':
                  color = '#98E1AE'
                  break;
                case '中文文献':
                  color = '#98E1AE'
                  break;
                case '溶瘤作用':
                  color = '#8DC8FF'
                  break;
                case '抗肿瘤免疫反应':
                  color = '#8DC8FF'
                  break;
                case '其他':
                  color = '#8DC8FF'
                  break;
                default:
                  color = '#8DC8FF'
                  break;
              }
              return color
            }
          }
        }
      })
    },
    getData () {
      return this.chartData.map(item => item.data)
    },
    getYAxis () {
      return this.chartData.map(item => item.name)
    }
  },
  watch: {
    chartData (val, old) {
      this.initChart()
    }
  },
  props: {
    elementId: {
      type: String,
      default: ''
    },
    chartTitle: {
      type: String,
      default: ''
    },
    chartData: {
      type: Array,
      default: () => []
    }
  }
}
</script>
<style lang="scss" scoped>
.bar-chart2-container {
  box-shadow: 5px 5px 6px #ccc;
  border-radius: 5px;
}
</style>